<template>
	<view>
		<view class="bg-white">
			<view style="login-top">
				<image style="width: 100vw;" src="../../static/login.jpg"></image>
			</view>
			<view class="padding login-bd form-cust">
				<view class="cu-form-group">
					<view class="title">手机号</view>
					<input placeholder="输入手机号" name="input" v-model="user.phoneNo"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">验证码</view>
					<input placeholder="输入短信验证码" name="input" v-model="user.checkCode"></input>
					<button v-if="send" class='cu-btn  shadow login-code' @tap="getCode">获取验证码</button>
					<button v-else class='cu-btn'> {{timerCount}}秒后重新获取</button>
				</view>
				<view class="text-center text-gray padding-top">
					<text>登录代表您同意</text>
					<text class="text-blue" @tap="sev">《用户注册协议》</text>
				</view>
				<view class="margin-top">
					<button type="primary" @tap="tologin">登 录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {isWx} from '../../utils/util.js';
	export default {
		data() {
			return {
				user: {
					phoneNo: '',
					checkCode: '',
					invitedCode: '',
					code: ''
				},
				send: true,
				timerCount: 60,
				inter: '',

			};
		},
		onLoad(option) {
			let Authorization = uni.getStorageSync('Authorization');
			if(Authorization){
				uni.reLaunch({
					url: '/pages/index/index'
				})
				return;
			}
			
			if(isWx()){
				if(option.code){
					this.user.code = option.code;
					this.user.invitedCode = uni.getStorageSync('invitedCode');
				}else{
					// 缓存邀请码
					uni.setStorageSync('invitedCode', option.id)
					// 判断是否绑定微信
					let host = window.location.protocol+"//"+window.location.host;
					window.location.replace(host + '/code.html?page=inter');
				}
			}else{
				if (option.id) {
					this.user.invitedCode = option.id;
				}
			}
		},
		methods: {
			sev(){
				uni.navigateTo({
					url: '/pages/login/server'
				})
			},
			getCode() {
				if (this.user.phoneNo == '' || (!(/^1[3456789]\d{9}$/.test(this.user.phoneNo)))) {
					uni.showToast({
						icon: 'none',
						title: '手机号错误'
					});
					return;
				}
				let _this = this;
				this.baseRequest('/sendMsg/' + this.user.phoneNo, 'POST', this.user, function(res, info) {
					uni.showToast({
						icon: 'none',
						title: '验证码已发送'
					})
					// _this.user.checkCode = '123456';
					_this.send = false;
					_this.inter = setInterval(() => {
						_this.count();
					}, 1000);
				});
			},
			count() {
				if (this.timerCount < 1) {
					clearInterval(this.inter);
					this.timerCount = 60;
					this.send = true;
					return;
				}
				this.timerCount--;
			},
			tologin() {
				let _this = this;
				this.baseRequest('/app/phoneSign', 'POST', this.user, function(res, info) {
					_this.userInfo = res.data;
					//this.userInfo= JSON.stringify(res.data);
					uni.setStorageSync('ONLINE_USER', _this.userInfo);
					uni.setStorageSync('Authorization', res.Authorization);
					//that.$store.commit("save",res);
					uni.reLaunch({
						url: '/pages/index/index'
					});
				})
			},
		}
	}
</script>

<style>
	.page {
		height: 100Vh;
		width: 100vw;
	}

	.page.show {
		overflow: hidden;
	}
</style>
